import { isUrl } from './verify';
import calculator from './calculator';

// 加减乘除
export const add = calculator.add.bind(calculator);
export const subtract = calculator.sub.bind(calculator);
export const multiply = calculator.mul.bind(calculator);
export const divide = calculator.div.bind(calculator);

/**
 * @intro: 工具类.
 */

/**
 * 获取UUID
 * @returns {string}  uuid
 */
export const uuid = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  // eslint-disable-next-line no-bitwise
  const r = Math.random() * 16 | 0;
  // eslint-disable-next-line no-bitwise,no-mixed-operators
  const v = c === 'x' ? r : (r & 0x3 | 0x8);
  return v.toString(16);
});

/**
 * 获取url请求的参数
 * @param url
 * @return {Object}
 */
export const getRequestUrlParams = (url) => {
  const result = {};

  if (!url) return result;
  const urlSearchParams = new URLSearchParams(url.substr(url.indexOf('?')));

  urlSearchParams.forEach(((value, key) => {
    result[key] = value;
  }));

  return result;
};
/**
 * 空函数
 */
export const empty = () => {
};

/**
 * 解析一个url地址(返回参数和location一样)
 * @param url
 */
export const parseURL = (url) => {
  if (!isUrl(url)) return {};

  const aTag = document.createElement('a');
  aTag.href = url;

  const {
    hash,
    host,
    hostname,
    href,
    origin,
    pathname,
    port,
    protocol,
    search,
  } = aTag;

  return {
    hash,
    host,
    hostname,
    href,
    origin,
    pathname,
    port,
    protocol,
    search,
    params: getRequestUrlParams(search),
  };
};

/**
 * 监听事件
 * @element dom
 * @event 事件名字
 * @handler 事件回调
 */
export const addEventListener = (() => {
  if (document.addEventListener) {
    // eslint-disable-next-line func-names
    return function (element, event, handler) {
      if (element && event && handler) {
        element.addEventListener(event, handler, false);
      }
    };
  }
  // eslint-disable-next-line func-names
  return function (element, event, handler) {
    if (element && event && handler) {
      element.attachEvent(`on${event}`, handler);
    }
  };
})();

/**
 * 移除事件
 * @element dom
 * @event 事件名字
 * @handler 事件回调
 */
export const removeEventListener = (() => {
  if (document.removeEventListener) {
    // eslint-disable-next-line func-names
    return function (element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false);
      }
    };
  }
  // eslint-disable-next-line func-names
  return function (element, event, handler) {
    if (element && event) {
      element.detachEvent(`on${event}`, handler);
    }
  };
})();

/**
 * 只监听一次事件
 * @element dom
 * @event 事件名字
 * @handler 事件回调
 */
export const addOnceEventListener = (el, event, fn) => {
  // eslint-disable-next-line func-names
  const listener = function () {
    // eslint-disable-next-line prefer-rest-params
    if (fn) fn.apply(this, arguments);

    removeEventListener(el, event, fn);
  };
  addEventListener(el, event, listener);
};

// 生成消息id
export const generateMsgId = () => String(new Date().getTime()) + Math.floor(Math.random() * 899 + 100);

/**
 * 生成聊天室token
 * @returns {string}
 */
export const generateChatToken = () => `${Math.floor(Math.random() * 8999 + 1000)}`;

/**
 * 获取一个device id
 */
export const getDeviceId = () => {
  // 客户端
  const deviceIdKey = 'x-device-id';
  const deviceIdStore = localStorage.getItem(deviceIdKey);
  if (deviceIdStore) return deviceIdStore;
  const rUuid = uuid();
  localStorage.setItem(deviceIdKey, rUuid);
  return rUuid;
};

/**
 *
 * @param {String|Number} number 数字 字符串
 * @param {Number} count 补全位数
 * @returns 补全后的数据
 */
export const paddingZero = (number, count = 2) => {
  const len = String(number).length;
  if (len < count) {
    const prefix = new Array(count - len).fill('0').join('');
    return `${prefix}${number}`;
  }
  return String(number);
};
